<?php

namespace backend\controllers;

use yii;
use backend\models\Baseconfigs;
use backend\models\Basemodule;

class ConfigsController extends CommonController
{
    /**
     * @param \yii\base\Action $action
     * @return bool
     * @throws \yii\web\BadRequestHttpException
     */
    public function beforeAction($action)
    {
        if (parent::beforeAction($action)) {
            if ($this->roleId > 2 && $this->roleId < 10){
                return false;
            }
            return true;
        } else {
            return false;
        }
    }

    public function actionIndex()
    {
        $moduleId = yii::$app->request->get('moduleId');
        $module = Basemodule::findOne(['Id' => $moduleId]);
        if (yii::$app->request->isPost){
            $post = yii::$app->request->post();
            $pageCurrent = $post['pageCurrent'];
            $pageSize = $post['pageSize'];
            $bconfig = new Baseconfigs();
            $data = $bconfig->getModuleConfig($post, $pageCurrent, $pageSize);
            return $this->renderJson($data);
        }
        $citem = Basemodule::getMoreRole($this->roleId, $moduleId);
        $data = [
            'module' => $module,
            'citem' => $citem
        ];
        return $this->renderPartial('index',$data);
    }


    public function actionCreate()
    {
        if (yii::$app->request->isPost){
            $post = yii::$app->request->post();
            if (empty($post['txt_ID'])){//添加
                $configs = new Baseconfigs();
                $result = $configs->insertData($post);
            }else{//修改
                $configs = Baseconfigs::findOne(['CfgKey' => $post['txt_ID']]);
                $result = $configs->updateData($post);
            }
            return $this->renderJson($result);
        }else{
            $id = yii::$app->request->get('id');
            $configKey = (empty($id)) ? '' : $id;
            $configs = Baseconfigs::findOne(['CfgKey'=>$configKey]);
            $data = [
                'configs' => $configs,
            ];
            return $this->renderPartial('create', $data);
        }
    }


    public function actionHandler()
    {
        if (yii::$app->request->isPost){
            $post = yii::$app->request->post();
            $configs = new Baseconfigs();
            $result = $configs->deleteData($post);
            return $this->renderJson($result);
        }
    }

}
